package com.bs.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bs.entity.AskLeave;
import com.bs.mapper.AskLeaveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

@Service
public class AskLeaveService extends ServiceImpl<AskLeaveMapper, AskLeave> {


    @Autowired
    ClockInfoService clockInfoService;

    public Boolean saveAskLeaveInfo(AskLeave askLeave) {
        // 获取传入的开始时间和结束时间，计算出时长
        String[] times = askLeave.getStartTime().split(",");
        askLeave.setStartTime(times[0]);
        askLeave.setEndTime(times[1]);
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
            Date firstDate = sdf.parse(times[0]);
            Date secondDate = sdf.parse(times[1]);
            Integer duration = Math.toIntExact(Math.abs(secondDate.getTime() - firstDate.getTime()) / 24 / 60 / 60 / 1000);
            Integer oldDuration = clockInfoService.getDuration();
            // 判断请假时长是否比设置的最长时长更加长
            if(oldDuration < duration){
                return false;
            }
            askLeave.setDuration(duration);
        }catch (Exception e){

        }
        return saveOrUpdate(askLeave);
    }
}
